package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApUnlikeBehaviorService;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.UnLikesBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.threadlocal.AppThreadLocalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class ApUnlikeBehaviorServiceImpl implements ApUnlikeBehaviorService {

    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;

    @Autowired
    MongoTemplate mongoTemplate;
    @Override
    public ResponseResult unlikeBehavior(UnLikesBehaviorDTO dto) {
        //判断是账号密码登录还是设备id登录
        ApUser user = AppThreadLocalUtils.getUser();
        if (user != null){
            //根据用户id,查询对应的实体
            ApBehaviorEntry ap = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), null);
            //根据实体id,文章Id增加不喜欢
            NolikeBehavior(ap.getId(),dto);
            //返回响应
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else {
            if (dto.getEquipmentId() != null){
                //根据用户id,查询对应的实体
                ApBehaviorEntry ap = apBehaviorEntryService.findByUserIdOrEquipmentId(null, dto.getEquipmentId());
                //根据实体id,文章Id增加不喜欢
                NolikeBehavior(ap.getId(),dto);
                //返回响应
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
            }else{
                CustException.cust(AppHttpCodeEnum.NEED_LOGIN, "当前用户未登录或者设备id不存在");
            }
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
    }

    private void NolikeBehavior(String id, UnLikesBehaviorDTO dto) {

        //查询不喜欢数据
        Query query = Query.query(Criteria.where("entryId").is(id).and("articleId").is(dto.getArticleId()).and("type").is((short)0));
        ApUnlikesBehavior one = mongoTemplate.findOne(query, ApUnlikesBehavior.class);

        if (dto.getType() == 0){//不喜欢
            //根据用户id,文章id查询数据是否存在
             if (one == null){
                //创建一条不喜欢数据
                ApUnlikesBehavior apUnlikesBehavior = new ApUnlikesBehavior();
                apUnlikesBehavior.setArticleId(dto.getArticleId());
                apUnlikesBehavior.setCreatedTime(new Date());
                apUnlikesBehavior.setEntryId(id);
                apUnlikesBehavior.setType((short)0);
                mongoTemplate.save(apUnlikesBehavior);
            }else{
                CustException.cust(AppHttpCodeEnum.DATA_EXIST);
            }
        }else if (dto.getType() == 1){ //取消不喜欢
            //判断不喜欢数据是否存在
            if (one == null){
                CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
            }else{
                //删除不喜欢的数据
                mongoTemplate.remove(query, ApUnlikesBehavior.class);
            }
        }
    }
}
